﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using SingleUserMall.Common;
using SingleUserMall.Videos.VO;

namespace SingleUserMall.Videos
{
    public class VideoAppService : SingleUserMallAppServiceBase, IVideoAppService
    {
        private readonly IRepository<Video, long> _videoRepository;

        public VideoAppService(IRepository<Video, long> videoRepository)
        {
            _videoRepository = videoRepository;
        }

        public async Task<PagedResultDto<VideoVO>> GetVideosAsync(PagedForm form)
        {
            var query = _videoRepository.GetAll().OrderByDescending(x=>x.CreationTime);
            return await query.ToPagedResultDtoAsync<Video, VideoVO>(form, (i, vo) => { });
        }
        public async Task<VideoVO> GetVideoAsync(SingleId<long> form)
        {
            var video = await _videoRepository.FirstOrDefaultAsync(form.Id);
            return video.ToVO<Video, VideoVO>((i, vo) => {  });
        }

        public async Task WatchVideoAsync(SingleId<long> form)
        {
            var video = await _videoRepository.FirstOrDefaultAsync(form.Id);
            video.WatchCount++;
        }
    }
}
